package org.galaxyworld.flycode.jobs.swing;

import org.galaxyworld.flycode.core.AbstractTask;
import org.galaxyworld.flycode.jobs.ModelGenerator;
import org.galaxyworld.flycode.utils.MessageHandler;

import javax.swing.*;
import java.util.List;

public class ModelGenerationTask extends AbstractTask<Void, String> {

    private JTextArea outputArea;

    public ModelGenerationTask(JTextArea outputArea) {
        this.outputArea = outputArea;
    }

    @Override
    protected Void doInBackground() throws Exception {
        ModelGenerator modelGenerator = new ModelGenerator();
        modelGenerator.setMessageHandler(new MessageHandler() {
            @Override
            public void onMessageReceived(String... messages) {
                publishMessage(messages);
            }
        });
        modelGenerator.generate();
        return null;
    }

    @Override
    protected void process(List<String> chunks) {
        for (String message : chunks) {
            outputArea.append(message + "\n");
            outputArea.setCaretPosition(outputArea.getText().length());
        }
    }
}
